package com.gzhryc.shared_device.common.payment.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.IProjectWalletService;
import com.gzhryc.shared_device.common.payment.dao.PayAccountGroupDao;
import com.gzhryc.shared_device.common.payment.dao.db.PayAccountGroup;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayConfig;
import com.gzhryc.shared_device.common.payment.services.dto.PayAccountGroupSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class PayAccountGroupService {

    static Logger log = Logger.getLogger(PayAccountGroupService.class);

    public static PayAccountGroupService self() {
        return MultiDBTools.getService(PayAccountGroupService.class, true);
    }

    public static PayAccountGroupService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PayAccountGroupService.class, true);
    }

    PayAccountGroupDao dao;

    public PayAccountGroupService(String jdbcKey) {
        this.dao = new PayAccountGroupDao(jdbcKey);
    }

    public List<PayAccountGroup> findAll(){
    	try {
            return dao.findAll();
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
    	return null;
    }
    
    public List<PayAccountGroup> findByUserId(Long userId){
    	if(userId != null) {
	    	Conditions condition = new Conditions(PayAccountGroup.class);
	    	condition.and().is("userId", userId);
	    	try {
	            return dao.findByCondition(condition);
	        } catch (SQLException e) {
	            log.error(e.getMessage(),e);
	        }
    	}
    	return null;
    }
    
    public List<PayAccountGroup> findBySearch(PayAccountGroupSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.desc("createDate");
        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(PayAccountGroupSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public PayAccountGroup getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(PayAccountGroup payAccountGroup,IProjectWalletService projectWalletService) throws LogicException{
        payAccountGroup.setCreateDate(new Date());

        JdbcSession.begin();
        try {
            if(dao.insert(payAccountGroup) > 0){
                //初始化项目钱包
                if(projectWalletService.add(payAccountGroup.getId())){
                    JdbcSession.commit();
                    return true;
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }finally {
            JdbcSession.end();
        }
        return false;
    }

    public boolean updateNotNull(PayAccountGroup payAccountGroup) throws LogicException{
        if(StringTools.isNotBlank(payAccountGroup.getWxTransferAppId())){
            WXPayConfig payConfig = WXPayConfigService.self().getByAppId(payAccountGroup.getWxTransferAppId());
            if(payConfig == null){
                throw new LogicException("微信提现应用ID不存在");
            }
            payAccountGroup.setWxTransferAppType(payConfig.getAppType());
        }
        try {
            return dao.updateNotNull(payAccountGroup) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        //检查是否有配置
        Long total = WXPayConfigService.self().countByAccountGroupId(id);
        if(total != null && total > 0){
            throw new LogicException("关联了微信支付配置，请先移除");
        }

        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
